#Cleaning Experiment 2
#Alan Yan
#3-16-2020

#clear environment
rm(list = ls())

#load libraries
library(pacman)
p_load(tidyverse)

#load data
dt <- read.csv("02-Experiment-2/data/03-post-experiment/raw_data.csv", header = TRUE, stringsAsFactors = FALSE)

#columns for each treatment
dt$male <- ifelse(dt$group.x == "male", 1, 0)
dt$female <- ifelse(dt$group.x == "female", 1, 0)
dt$gen.neutral <- ifelse(dt$group.x == "gen.neutral", 1, 0)
dt$no.name <- ifelse(dt$group.x == "no.name", 1, 0)

#noncompliance
dt$male.instrument <- ifelse(dt$conversation_status != "start", 
                             dt$male, 0)
dt$female.instrument <- ifelse(dt$conversation_status != "start", 
                               dt$female, 0)
dt$gen.neutral.instrument <- ifelse(dt$conversation_status != "start", 
                                    dt$gen.neutral, 0)
dt$no.name.instrument <- ifelse(dt$conversation_status != "start", 
                                dt$no.name, 0)

#silencing variable
dt$silenced <- ifelse(dt$opted_out == TRUE, 100, 0)

#attendance variable
dt$call <- ifelse(dt$Will.they.call...Yes == "Yes", 100, 0)

#responded
dt$responded <- ifelse(dt$num_messages > 1, 100, 0)

#check reliability of offensiveness ratings
#if there are missing ratings, code them as 1 for least offensive and least discouraging
dt$offensive.x <- ifelse(is.na(dt$offensive.x) == TRUE, 1, dt$offensive.x)
dt$offensive.y <- ifelse(is.na(dt$offensive.y) == TRUE, 1, dt$offensive.y)

dt$encouraging.x <- ifelse(is.na(dt$encouraging.x) == TRUE, 1, dt$encouraging.x)
dt$encouraging.y <- ifelse(is.na(dt$encouraging.y) == TRUE, 1, dt$encouraging.y)

#check reliability of offensiveness
summary(lm(dt$offensive.x~dt$offensive.y))$r.squared %>% sqrt()
#raw correlation = .74

#check reliability of discouragingness
summary(lm(dt$encouraging.x~dt$encouraging.y))$r.squared %>% sqrt()
#raw alpha = .95

#These are encouraging results so let's sum and then normalize these measures
dt$offensive.index <- (dt$offensive.x + dt$offensive.y - 2)/8 *100
dt$discouraging.index <- (dt$encouraging.x + dt$encouraging.y - 2)/12 *100

#silencing
dt$pure.silencing <- ifelse(dt$silenced == 100 & dt$offensive.index > 0, 100, 0)

#write csv
write.csv(dt, "02-Experiment-2/data/04-clean-data/clean_data.csv")
